//package com.own.demo.geodemo;
//
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.own.demo.utils.*;
//import com.own.demo.utils.geo.PoiTypeEnums;
//import com.own.demo.utils.geo.buffer.DefaultBufferImpl;
//import com.own.demo.utils.geo.buffer.LineBufferImpl;
//import com.own.demo.utils.geo.buffer.PolygonBufferImpl;
//import com.own.demo.vo.Bounds;
//import lombok.SneakyThrows;
//import org.dom4j.Document;
//import org.dom4j.DocumentHelper;
//import org.dom4j.Element;
//import org.junit.Test;
//import org.locationtech.jts.geom.*;
//
//import java.io.BufferedWriter;
//import java.io.File;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.text.ParseException;
//import java.util.*;
//import java.util.concurrent.atomic.AtomicLong;
//import java.util.stream.Collectors;
//
//public class ToGeo点信息 {
//    //边界
//    public static  long indexPointIdIndex = 1;
//    static AtomicLong allPint02Index=new AtomicLong();
//
//
//
//    static int HEIGHT = 100;
//    static int AREA = 1;
//    static LineBufferImpl defaultBuffer = (LineBufferImpl) PoiTypeEnums.POLYLINE.getBufferUtil();
//    static PolygonBufferImpl polygonBuffer = new PolygonBufferImpl();
//
//    static String buld = "112.671689,28.246673;112.750619,28.295456;112.836473,28.239354;112.826780,28.166133;112.727078,28.150262;112.632916,28.164912;112.671689,28.246673&113.722709,28.356403;113.823796,28.347873;113.809948,28.26131;113.724094,28.313744;113.722709,28.356403";
//    static String[] noPoint = "112.788572,28.244334;113.593521,28.182595;113.314825,28.334820;113.325724,28.037024;112.785673,28.181760;112.783130,28.167562".split("\\;");
//
//    public static boolean isInPolygon(Polygon polygon) {
//        String data = polygonBuffer.gpsToString(polygon.getCoordinates());
//        for (int i = 0; i < noPoint.length; i++) {
//            String point = noPoint[i];
//            if (RegionUtil.polygonCheck(point, data)) {
//                return true;
//            }
//        }
//        return false;
//    }
//
//    /**
//     * @param geometries 分成格子边界边界数据
//     * @param geometry   全量边界
//     * @param bounds
//     * @param flag       false 只生成边界数据。 不生成osm文件  true 全量生成
//     * @return
//     */
//    @SneakyThrows
//    public static String goGeoJSONV2(List<Geometry> geometries, Geometry geometry, Bounds bounds, boolean flag) {
//        if (geometries == null) {
//            return null;
//        }
//        int row = geometries.size();
//        List<Coordinate> allPoints = new ArrayList<>(100000);
//        Map<String, Long> allPint02Map = new HashMap<>(1024);
//
//
//
//        for (int i = 0; i < row; i++) {
//            Polygon js = (Polygon) geometries.get(i);
//            if (!isInPolygon(js)) {
//                Coordinate[] coordinates = js.getEnvelope().getCoordinates();
//               for(int j=0;j<coordinates.length;j++){
//                   Coordinate coordinate1=coordinates[j];
//                   String key= coordinate1.x+"_"+coordinate1.y;
//                   if(!allPint02Map.containsKey(key)){
//                       allPint02Map.put(key,allPint02Index.getAndIncrement());
//                   }
//               }
//            }
//        }
//
////        List<List<List<Coordinate>>> list = DiagonalUtils.diagonalGeo(allPint02, centerPoint02, allPint13);
////
////        int allPointsLon = allPoints.size();
////        System.out.println("点数量：" + allPoints.size());
////
////        List<Double> lonList = new ArrayList<>(allPointsLon);
////        List<Double> latList = new ArrayList<>(allPointsLon);
////
////        for (int i = 0; i < allPointsLon; i++) {
////            Coordinate coordinate = allPoints.get(i);
////            lonList.add(coordinate.x);
////            latList.add(coordinate.y);
////        }
////        System.out.println("x去重前的数量：" + lonList.size());
////        System.out.println("y去重前的数量：" + latList.size());
////        lonList = lonList.stream().distinct().collect(Collectors.toList());
////        latList = latList.stream().distinct().collect(Collectors.toList());
////        System.out.println("x去后前的数量：" + lonList.size());
////        System.out.println("y去后前的数量：" + latList.size());
////
////        int lonListRow = lonList.size();
////        Map<String, List<Coordinate>> xMap = new LinkedHashMap(1024);
////        for (int i = 0; i < lonListRow; i++) {
////            List<Coordinate> data = new ArrayList<>(1024);
////            String tempLon = lonList.get(i) + "";
////            for (int j = 0; j < allPointsLon; j++) {
////                Coordinate coordinate = allPoints.get(j);
////                String x = coordinate.x + "";
////                if (tempLon.equals(x)) {
////                    data.add(coordinate);
////                }
////            }
////
////            data = data.stream().distinct().collect(Collectors.toList());
////            data = data.stream().sorted(Comparator.comparing(Coordinate::getY)).collect(Collectors.toList());
////            xMap.put(tempLon, data);
////        }
////
////
////        //获取y线
////        int latListRow = latList.size();
////
////        Map<String, List<Coordinate>> yMap = new LinkedHashMap(1024);
////        for (int i = 0; i < latListRow; i++) {
////            List<Coordinate> dataLat = new ArrayList<>(1024);
////            String tempLat = latList.get(i) + "";
////            for (int j = 0; j < allPointsLon; j++) {
////                Coordinate coordinate = allPoints.get(j);
////                String y = coordinate.y + "";
////                if (tempLat.equals(y)) {
////                    dataLat.add(coordinate);
////                }
////            }
////
////            dataLat = dataLat.stream().distinct().collect(Collectors.toList());
////            dataLat = dataLat.stream().sorted(Comparator.comparing(Coordinate::getY)).collect(Collectors.toList());
////            yMap.put(tempLat, dataLat);
////        }
////
////
////        System.out.println("DELETE from geofence where geofence_id>34669 and service_id=713;");
////        for (Map.Entry<String, List<Coordinate>> v : xMap.entrySet()) {
////            List<Coordinate> value = v.getValue();
//////            getSql(value);
////        }
////
////        System.out.println("-- y线");
////        for (Map.Entry<String, List<Coordinate>> v : yMap.entrySet()) {
////            List<Coordinate> value = v.getValue();
//////            getSql(value);
////        }
////
////        List<List<Coordinate>> allXYdata = new ArrayList<>(1026);
////        List<Coordinate> listAll = Arrays.asList(geometry.getCoordinates());
////
////        getSql(listAll);
////        allXYdata.add(listAll);
////        allXYdata.addAll(xMap.values());
////        allXYdata.addAll(yMap.values());
////
////        List<List<Coordinate>>  xy=  ToGeo对角线2.goGeoJSONV2(geometries,geometry,bounds,flag);
////        String xmlData = dataDramXml(allXYdata, bounds, xMap, yMap, list, flag,xy);
////        System.out.println(xmlData.length() % 1024 % 1024);
////        if (flag) {
////            writerFoFileOSM(xmlData);
////        }
////        allXYdata.addAll(xy);
////        String json = dataDraw(allXYdata, bounds, list, flag);
////        writerFoFile("var polylineData1 = " + json);
////        return json;
//        return null;
//    }
//
//
//    public static void writerFoFileOSM(String data) {
//        try {
//            String folderPath = "E:\\ownCode\\graphhopper-web-7.0\\graphhopper-master";
//            File folder = new File(folderPath);
//            String fileName = "\\changsha-36.osm";
//            if (!folder.exists()) {
//                boolean result = folder.mkdirs();
//            }
//            BufferedWriter bufferedWriter = CommWriterFileInfo.getWriterBuffer(folderPath + "\\" + fileName, false);
//            CommWriterFileInfo.writer(bufferedWriter, data);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    public static void writerFoFile(String data) {
//        try {
//            String folderPath = "E:\\ownCode\\graphhopper-web-7.0\\地图工具";
//            File folder = new File(folderPath);
//            String fileName = "\\polylineData1.js";
//            if (!folder.exists()) {
//                boolean result = folder.mkdirs();
//            }
//            BufferedWriter bufferedWriter = CommWriterFileInfo.getWriterBuffer(folderPath + "\\" + fileName, false);
//            CommWriterFileInfo.writer(bufferedWriter, data);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static String dataDramXml(List<List<Coordinate>> list, Bounds bounds,
//                                     Map<String, List<Coordinate>> xMap,
//                                     Map<String, List<Coordinate>> yMap,
//                                     List<List<List<Coordinate>>> lists,
//                                     boolean flag,
//                                     //对角线xy
//                                     List<List<Coordinate>> xyDataList
//    ) {
//        //创建document对象
//        Document document = DocumentHelper.createDocument();
//        //添加节点
//        Element osm = document.addElement("osm");
//        osm.addAttribute("version", "0.6");
//        osm.addAttribute("generator", "CGImap 0.8.8 (587855 spike-08.openstreetmap.org)");
//        osm.addAttribute("copyright", "OpenStreetMap and contributors");
//        osm.addAttribute("attribution", "http://www.openstreetmap.org/copyright");
//        osm.addAttribute("license", "http://opendatacommons.org/licenses/odbl/1-0/");
//
//
//        Element boundsEl = osm.addElement("bounds");
//        boundsEl.addAttribute("minlat", bounds.getMinLat() + "");
//        boundsEl.addAttribute("minlon", bounds.getMaxLon() + "");
//        boundsEl.addAttribute("maxlat", bounds.getMaxLat() + "");
//        boundsEl.addAttribute("maxlon", bounds.getMaxLon() + "");
//        boundsEl.addAttribute("id", ToGeoJSONV2.indexPointIdIndex + "");
//        boundsEl.addAttribute("version", "1");
//
//        Map<String, Long> indexToGPS = new HashMap<>();
//        int row = list.size();
//
//        for (int i = 0; i < row; i++) {
//            List<Coordinate> temp = list.get(i);
//            Coordinate[] coordinates = temp.toArray(new Coordinate[0]);
//            int coordRow = coordinates.length;
//            for (int h = 0; h < coordRow; h++) {
//                ToGeoJSONV2.indexPointIdIndex = ToGeoJSONV2.indexPointIdIndex + 1;
//                setElement(osm, ToGeoJSONV2.indexPointIdIndex, coordinates[h], indexToGPS);
//            }
//        }
//
//
//        String[] build = buld.split("&");
//        int buildRow = build.length;
//        for (int i = 0; i < buildRow; i++) {
//            String[] points = build[i].split("\\;");
//            for (int h = 0; h < points.length; h++) {
//                ToGeoJSONV2.indexPointIdIndex = ToGeoJSONV2.indexPointIdIndex + 1;
//                String[] arr = points[h].split("\\,");
//                Coordinate coordinate = new Coordinate(Double.valueOf(arr[0]), Double.valueOf(arr[1]));
//                setElement(osm, ToGeoJSONV2.indexPointIdIndex, coordinate, indexToGPS);
//            }
//        }
//
//
//        for (Map.Entry<String, List<Coordinate>> v : xMap.entrySet()) {
//            ToGeoJSONV2.indexPointIdIndex = ToGeoJSONV2.indexPointIdIndex + 1;
//            setWay(osm, ToGeoJSONV2.indexPointIdIndex, indexToGPS, v.getValue(), "highway", "motorway");
//        }
//
//        for (Map.Entry<String, List<Coordinate>> v : yMap.entrySet()) {
//            ToGeoJSONV2.indexPointIdIndex = ToGeoJSONV2.indexPointIdIndex + 1;
//            setWay(osm, ToGeoJSONV2.indexPointIdIndex, indexToGPS, v.getValue(), "highway", "motorway");
//        }
//
//        if (flag) {
//            for (int i = 0; i < lists.size(); i++) {
//                List<List<Coordinate>> data = lists.get(i);
//                for (int h = 0; h < data.size(); h++) {
//                    List<Coordinate> listInfo = data.get(h);
//                    ToGeoJSONV2.indexPointIdIndex = ToGeoJSONV2.indexPointIdIndex + 1;
//                    setWay(osm, ToGeoJSONV2.indexPointIdIndex, indexToGPS, listInfo, "highway", "motorway");
//                }
//            }
//        }
//        for (int i = 0; i < buildRow; i++) {
//            String[] points = build[i].split("\\;");
//            List<Coordinate> value = new ArrayList<>();
//            for (int h = 0; h < points.length; h++) {
//                ToGeoJSONV2.indexPointIdIndex = ToGeoJSONV2.indexPointIdIndex + 1;
//                String[] arr = points[h].split("\\,");
//                value.add(new Coordinate(Double.valueOf(arr[0]), Double.valueOf(arr[1])));
//            }
//            setWay(osm, ToGeoJSONV2.indexPointIdIndex, indexToGPS, value, "building", "warehouse");
//        }
//
//        if(xyDataList != null && xyDataList.size()>0){
//            for (int i = 0; i < xyDataList.size(); i++) {
//                List<Coordinate> coordinates=xyDataList.get(i);
//                ToGeoJSONV2.indexPointIdIndex = ToGeoJSONV2.indexPointIdIndex + 1;
//                setWay(osm, ToGeoJSONV2.indexPointIdIndex, indexToGPS, coordinates, "highway", "motorway");
//            }
//        }
//        return document.asXML();
//    }
//
//    public static void setWay(Element osm, long indexPointId, Map<String, Long> indexToGPS, List<Coordinate> value, String k, String v) {
//        Element way = osm.addElement("way");
//        way.addAttribute("id", indexPointId + "");
//        way.addAttribute("visible", true + "");
//        way.addAttribute("version", 1 + "");
//
//        for (int h = 0; h < value.size(); h++) {
//            indexPointId = indexPointId + 1;
//            Element nod = way.addElement("nd");
//            Coordinate coordinate = value.get(h);
//            String key = coordinate.getX() + "-" + coordinate.getY();
//            nod.addAttribute("ref", indexToGPS.get(key) + "");
//        }
//
//        Element tag = way.addElement("tag");
//        tag.addAttribute("k", k);
//        tag.addAttribute("v", v);
//
//        String name="称"+indexPointId;
//        Element tagName = way.addElement("tag");
//        tagName.addAttribute("k","name");
//        tagName.addAttribute("v", name);
//
//
//        String street_name ="路"+indexPointId;
//        Element stree = way.addElement("tag");
//        stree.addAttribute("k","street_name");
//        stree.addAttribute("v", street_name);
//
//
//        String street_name1 ="街道名称—驼峰"+indexPointId;
//        Element stree1= way.addElement("tag");
//        stree1.addAttribute("k","streetName");
//        stree1.addAttribute("v", street_name1);
//
//
//        Element stree1Id  = way.addElement("tag");
//        stree1Id.addAttribute("k","ID");
//        stree1Id.addAttribute("v", indexPointId+"");
//
//        Element fme_feature_type  = way.addElement("tag");
//        fme_feature_type.addAttribute("k","fme_feature_type");
//        fme_feature_type.addAttribute("v", indexPointId+"_英");
//    }
//
//
//    public static void setElement(Element osm, long indexPointId, Coordinate coordinate, Map<String, Long> indexToGPS) {
//        Element node1 = osm.addElement("node");
//        node1.addAttribute("id", indexPointId + "");
//        node1.addAttribute("visible", true + "");
//        node1.addAttribute("version", 1 + "");
//
//        node1.addAttribute("lat", coordinate.getY() + "");
//        node1.addAttribute("lon", coordinate.getX() + "");
//        indexToGPS.put(coordinate.getX() + "-" + coordinate.getY(), indexPointId);
//
//
//
//        String name="名"+indexPointId;
//        Element tagName = node1.addElement("tag");
//        tagName.addAttribute("k","name");
//        tagName.addAttribute("v", name);
//
//
//        String street_name ="街"+indexPointId;
//        Element stree = node1.addElement("tag");
//        stree.addAttribute("k","street_name");
//        stree.addAttribute("v", street_name);
//
//
//        String street_name1 ="名称—驼峰"+indexPointId;
//        Element stree1= node1.addElement("tag");
//        stree1.addAttribute("k","streetName");
//        stree1.addAttribute("v", street_name1);
//
//
//        Element stree1Id  = node1.addElement("tag");
//        stree1Id.addAttribute("k","ID");
//        stree1Id.addAttribute("v", indexPointId+"");
//
//        Element fme_feature_type  = node1.addElement("tag");
//        fme_feature_type.addAttribute("k","fme_feature_type");
//        fme_feature_type.addAttribute("v", indexPointId+"_中文");
//    }
//
//
//
//    public static String dataDraw(List<List<Coordinate>> list, Bounds bounds, List<List<List<Coordinate>>> listInfo, boolean flag) {
//        JSONObject jsonObject = new JSONObject();
//        JSONArray jsonArray = new JSONArray(1024);
//        jsonObject.put("type", "GeometryCollection");
//        if (flag) {
//            for (int i = 0; i < listInfo.size(); i++) {
//                list.addAll(listInfo.get(i));
//            }
//        }
//        int row = list.size();
//        for (int j = 0; j < AREA; j++) {
//            for (int i = 0; i < row; i++) {
//                jsonArray.add(setGeoJSON(i, j, list.get(i), "LineString"));
//            }
//        }
//
//        String[] build = buld.split("&");
//        int buildRow = build.length;
//        for (int i = 0; i < buildRow; i++) {
//            String[] points = build[i].split("\\;");
//            List<Coordinate> value = new ArrayList<>();
//            for (int h = 0; h < points.length; h++) {
//                String[] arr = points[h].split("\\,");
//                value.add(new Coordinate(Double.valueOf(arr[0]), Double.valueOf(arr[1])));
//            }
//            jsonArray.add(setGeoJSON(i, i, value, "LineString"));
//        }
//
//        jsonObject.put("features", jsonArray);
//        return jsonObject.toJSONString();
//    }
//
//    public static JSONObject setGeoJSON(int i, int j, List<Coordinate> list, String geoType) {
//        JSONObject geo = new JSONObject(4);
//        geo.put("type", "Feature");
//        JSONObject geoPro = new JSONObject(1);
//        geoPro.put("name", i > 0 ? j + "-" + i : "area");
//        geo.put("properties", geoPro);
//        JSONObject objectInfo = new JSONObject();
//        Coordinate[] coordinates = list.toArray(new Coordinate[0]);
//        int coordRow = coordinates.length;
//        List<double[]> gpsList = new ArrayList<>(coordRow);
//
//        for (int h = 0; h < coordRow; h++) {
//            Coordinate coordinate = coordinates[h];
//            double[] gps = new double[2];
//            gps[0] = coordinate.x;
//            gps[1] = coordinate.y;
//            gpsList.add(gps);
//        }
//        objectInfo.put("coordinates", gpsList);
//        objectInfo.put("type", geoType);
//        geo.put("geometry", objectInfo);
//        return geo;
//    }
//
//    public static String getSql(List<Coordinate> geometry) {
//        String dataPoint = defaultBuffer.gpsToString(geometry.toArray(new Coordinate[0]));
//        Geometry gps = defaultBuffer.geometry(geometry.toArray(new Coordinate[0]));
//        String sql = "INSERT INTO `geofence` (" +
//                "`service_id`," +
//                "`ak`," +
//                "`cert_info_id`," +
//                "`geofence_name`," +
//                "`geofence_shape`," +
//                "`coordinates_buffer`," +
//                "`end_time`," +
//                "`start_time`," +
//                "`deleted`," +
//                "`group_id`," +
//                "`fence_create_type`," +
//                "`coordinates`," +
//                "`center`," +
//                "`radius`," +
//                "`poi_id`," +
//                "`bounds`," +
//                "`buffer`," +
//                "`judge_type`," +
//                "`status`," +
//                "`audit_info`," +
//                "`money`," +
//                "`audit_time`," +
//                "`audit_user_id`," +
//                "`create_time`," +
//                "`warning_count`," +
//                "`geometry_info`," +
//                "`adcode`," +
//                "`max_geometry` " +
//                ")" +
//                "VALUES" +
//                "(" +
//                "" +
//                "713," +
//                "'FD1635824265606119417A4631F30DB04342A86E12D485369E64NSZETCMM7254'," +
//                "254," +
//                "'  '," +
//                "'polyline'," +
//                "'" + dataPoint + "'," +
//                "NULL," +
//                "NULL," +
//                "0," +
//                "NULL," +
//                "'0'," +
//                "'" + dataPoint + "'," +
//                "NULL," +
//                "NULL," +
//                "NULL," +
//                "NULL," +
//                "0.00," +
//                "NULL," +
//                "0," +
//                "NULL," +
//                "0.00," +
//                "NULL," +
//                "NULL," +
//                "'2022-07-16 16:45:46'," +
//                "0," +
//                "ST_GeomFromText ( '" + gps + "' )," +
//                "NULL," +
//                "NULL " +
//                ");";
//        System.out.println(sql);
//        return sql;
//    }
//}
